<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>php 扩展模块如何安装 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>php 扩展模块如何安装</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/LAMP/">LAMP</a> <a class="article__tag-link" href="/tags/Php/">Php</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;php 和 apapche 类似，核心文件为 /usr/local/php/bin/php ,针对 apache 的是 /usr/local/apache2/modules/libphp5.so 模块。这两个文件是核心，在编译 php 的时候会提前让它支持一些功能，比如支持 mysql，这个功能其实是 php 的一个模块，只不过这个模块是直接和 php 或者 libphp5.so 文件编译在一起的。当我们编译完成 php 后，发现还需要让 php 支持另外的模块，这时候可以重新编译 php ，加上配置参数，或者直接编译出一个独立的模块文件。然后让 php去调用它。下面以编译 memcache 为例；</p>
<p>1.下载 memcache 源码包</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@lamp src]<span class="comment"># wget http://www.apelearn.com/bbs/data/attachment/forum/memcache-2.2.3.tgz</span></div></pre></td></tr></table></figure>
<ol>
<li>安装</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@lamp src]<span class="comment"># tar zxvf memcache-2.2.3.tgz </span></div><div class="line">[root@lamp src]<span class="comment"># cd memcache-2.2.3</span></div><div class="line">[root@lamp memcache-2.2.3]<span class="comment"># /usr/local/php/bin/phpize</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明：这一步是借助 php 的 phpize 工具生成 configure 文件。在这一步可能会遇到一些错误，不如 “Cannot find config.m4”，这是因为系统还没有安装 m4 工具，使用 yum 安装</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@lamp ~]<span class="comment"># yum install -y m4</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;还有错误“Cannot find autoconf” ，解决办法是</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">[root@lamp ~]<span class="comment"># yum install -y autoconf</span></div><div class="line"></div><div class="line">[root@lamp memcache-2.2.3]<span class="comment"># ./configure --with-php-config=/usrl/local/php/bin/php-config</span></div><div class="line">[root@lamp memcache-2.2.3]<span class="comment"># make &amp;&amp; make install</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;说明：当 make install 后会生成一个 memcache.so 的模块文件，该文件在 php 的 extension_dir 下。查看 php extension_dir 的方法是</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@lamp ~]<span class="comment"># /usr/local/php/bin/php -i | grep extension_dir</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/php%20%E6%89%A9%E5%B1%95%E6%A8%A1%E5%9D%97%E5%A6%82%E4%BD%95%E5%AE%89%E8%A3%85/01.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改 extension_dir 的方法是，编辑 php.ini 文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@lamp lamp]<span class="comment"># vim /usr/local/php/etc/php.ini</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/php%20%E6%89%A9%E5%B1%95%E6%A8%A1%E5%9D%97%E5%A6%82%E4%BD%95%E5%AE%89%E8%A3%85/02.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改为（一般情况不做修改）</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/php%20%E6%89%A9%E5%B1%95%E6%A8%A1%E5%9D%97%E5%A6%82%E4%BD%95%E5%AE%89%E8%A3%85/03.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后继续在 php.ini 中添加</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">extension=memcache.so</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/php%20%E6%89%A9%E5%B1%95%E6%A8%A1%E5%9D%97%E5%A6%82%E4%BD%95%E5%AE%89%E8%A3%85/04.png?raw=true" alt=""></figure></p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/php%20%E6%89%A9%E5%B1%95%E6%A8%A1%E5%9D%97%E5%A6%82%E4%BD%95%E5%AE%89%E8%A3%85/05.png?raw=true" alt=""></figure></p>
<h2 id="还有就是重新动态编译-php-自带模块"><a href="#还有就是重新动态编译-php-自带模块" class="headerlink" title="还有就是重新动态编译 php 自带模块"></a>还有就是重新动态编译 php 自带模块</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;首先找到 php 的源码包</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@lamp ~]<span class="comment"># cd /usr/local/src/php-5.6.6</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;很多模块的源码都是 /ext 目录下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@lamp php-5.6.6]<span class="comment"># cd ext/</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/php%20%E6%89%A9%E5%B1%95%E6%A8%A1%E5%9D%97%E5%A6%82%E4%BD%95%E5%AE%89%E8%A3%85/06.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;现在编译一下 curl 模块</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@lamp ext]<span class="comment"># /usr/local/php/bin/php -m | grep -i curl</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;进入 curl 目录</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@lamp ext]<span class="comment"># cd curl/</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/php%20%E6%89%A9%E5%B1%95%E6%A8%A1%E5%9D%97%E5%A6%82%E4%BD%95%E5%AE%89%E8%A3%85/07.png?raw=true" alt=""></figure></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@lamp curl]<span class="comment"># /usr/local/php/bin/phpize</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/php%20%E6%89%A9%E5%B1%95%E6%A8%A1%E5%9D%97%E5%A6%82%E4%BD%95%E5%AE%89%E8%A3%85/08.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;上面这步是为了生成  configure 文件，没有这个文件是没办法编译的</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/php%20%E6%89%A9%E5%B1%95%E6%A8%A1%E5%9D%97%E5%A6%82%E4%BD%95%E5%AE%89%E8%A3%85/09.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编译</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@lamp curl]<span class="comment"># ./configure --with-php-config=/usr/local/php/bin/php-config</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;安装</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@lamp curl]<span class="comment"># make &amp;&amp; make install</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编译安装完成，最后一排会有模块目录</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/php%20%E6%89%A9%E5%B1%95%E6%A8%A1%E5%9D%97%E5%A6%82%E4%BD%95%E5%AE%89%E8%A3%85/10.png?raw=true" alt=""></figure></p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/php%20%E6%89%A9%E5%B1%95%E6%A8%A1%E5%9D%97%E5%A6%82%E4%BD%95%E5%AE%89%E8%A3%85/11.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;加载模块</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编辑 php.ini</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@lamp curl]<span class="comment"># vim /usr/local/php/etc/php.ini</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;增加内容</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">extension=curl.so</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/php%20%E6%89%A9%E5%B1%95%E6%A8%A1%E5%9D%97%E5%A6%82%E4%BD%95%E5%AE%89%E8%A3%85/12.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;查看是否加载成功</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/php%20%E6%89%A9%E5%B1%95%E6%A8%A1%E5%9D%97%E5%A6%82%E4%BD%95%E5%AE%89%E8%A3%85/13.png?raw=true" alt=""></figure></p>

	

	
		<span class="different-posts"><a href="/2017/10/12/Php/12. php 扩展模块如何安装/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
